package com.lemner.motion.activity;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.lemner.motion.R;
import com.lemner.motion.base.BaseActivity;
import com.lemner.motion.base.BaseListener;
import com.lemner.motion.base.BaseModel;
import com.lemner.motion.base.URL;
import com.lemner.motion.bean.BaseBean;
import com.lemner.motion.util.ActivityConllector;
import com.lemner.motion.util.FullScreen;


public class ChangePwdActivity extends BaseActivity {
private ImageView iv_back;
private EditText et_old,et_new,et_renew;
private Button  btn_done;
private String id;


    @Override
    public int getLayoutFile() {
        return R.layout.activity_change_pwd;
    }

    @Override
    public void initView() {
        FullScreen.fullScreen(this,0x0020c26f);
        iv_back=(ImageView)findViewById(R.id.iv_back);
        et_old=(EditText)findViewById(R.id.et_old);
        et_new=(EditText)findViewById(R.id.et_new);
        et_renew=(EditText)findViewById(R.id.et_renew);
        btn_done=(Button)findViewById(R.id.btn_done);
    }

    @Override
    public void initEvent() {
       iv_back.setOnClickListener(this);
       btn_done.setOnClickListener(this);
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        Intent intent=new Intent(ChangePwdActivity.this,UserSettingActivity.class);
        startActivity(intent);
        finish();
    }

    @Override
    public void initData() {
        id=getIntent().getStringExtra("studentid");
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.iv_back:
                Intent intent=new Intent(ChangePwdActivity.this,UserSettingActivity.class);
                startActivity(intent);
                finish();
                break;
            case R.id.btn_done:
                String old=et_old.getText().toString();
                String new1=et_new.getText().toString();
                String renew=et_renew.getText().toString();
                if(TextUtils.isEmpty(old)||TextUtils.isEmpty(new1)||TextUtils.isEmpty(renew))
                    showToast("输入信息有空，请检查后再试");
                else if(!new1.equals(renew))
                    showToast("两次密码不一致");
                else
                    updatepwd(URL.md5(old),URL.md5(renew));
        }
    }

    BaseListener<BaseBean>listener=new BaseListener<BaseBean>() {
        @Override
        public void onResponse(BaseBean baseBean) {
            if(baseBean.getErrorCode().equals("0000"))
                forceonline(ChangePwdActivity.this);
            else if(baseBean.getReturnCode().equals("0001")) {
                showToast("修改密码成功");
                ActivityConllector.finishAll();//销毁所有的活动
                Intent intent1 = new Intent(ChangePwdActivity.this, LoginActivity.class);
                startActivity(intent1);
            }
            else
                showToast(baseBean.getErrorMsg());
        }

        @Override
        public void onFail(String msg) {
                showToast(msg);
        }
    };

    public void updatepwd(String old,String renew){
        BaseModel<BaseBean> model=new BaseModel<>();
        retrofit2.Call<BaseBean>call=  model.service.updatepwd(id,renew, old,LoginActivity.session);
        model.callEnqueue(call,listener);
    }

}
